<?php
/**
 * 不参加统计操作类
 *
 * @version 1.0
 * @date 2013-12-13
 */
class DetailController extends FController {
	public function accessRules() {
		$config = array('index' => 1);
		return ZRole::auth("nojoinrule_detail", $config);
	}
	public function __common() {
		parent::init();
		if (empty($this->cLoginUser)) {
			$this->redirect($this->createUrl('/site/login'));
		}
		$this->commonScript();
		Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
	}
	//列表显示
	public function actionIndex() {
		$this->__common();
		$conn = Yii::app()->db;
		$s['rowname'] = Yii::app()->request->getParam('rowname');
		$s['tablerowname'] = Yii::app()->request->getParam('tablerowname');
		$s['status'] = Yii::app()->request->getParam('status');
		$s['id'] = Yii::app()->request->getParam('id');
		$where = ' AND "import_id"="'.$s['id'].'"';
		$order = ' ORDER BY "id" DESC';
		$limit = 8;
		# 拼接where #
		if(!empty($s['rowname'])){
			$where.= ' AND "rowname" like "%'.$s['rowname'].'%"';
		}
		if(!empty($s['tablerowname'])){
			$where.= ' AND "tablerowname" like "%'.$s['tablerowname'].'%"';
		}
		if(!empty($s['status'])){
			echo $s['status'];
			$where.= ' AND "status" = "1"';
		}
		$sql = 'SELECT * FROM {{importrule_detail}} WHERE 1 '.$where.$order;
		$count = 'SELECT COUNT("id") FROM {{importrule_detail}} WHERE 1 '.$where;
		$data = ZCommon::getDaoPageList($sql, $count, $limit);
		$pageUrl = $this->createUrl('/report_rule/default/index');
		$this->render('index',array(
		'data'	=>$data['listDatas'],
		'pages' => $data['pages'],
		'serch'	=> $s,
		'pageUrl' => $pageUrl
		));
	}
	//更新
	public function actionUpdate(){
		//$this->__common();
		$this->layout = "//layouts/main_window";
		$id = (int)Yii::app()->request->getparam('id');
		$id = (!empty($_POST['info']['id'])) ? $_POST['info']['id'] : $id;
		$model = ImportruleDetail::model()->findByPk($id);
		if(empty($model)){
			$this->runShowMessge("index",'数据出错');
			exit;
		}
		if(!empty($_POST['submit'])){
			$data = $_POST['info'];
			$data['status'] = (!empty($data['status'])) ? $data['status'] : 0;
			$model->rowname=$data['rowname'];
			$model->tablerowname=$data['tablerowname'];
			$model->default=$data['default'];
			$model->status=$data['status'];
			$model->remark=$data['remark'];
			if($model->save()){
				$this->runShowMessge("index");
				//$this->redirect('/import_rule/detail/index',array('id'=>$id));
				$this->redirect($this->createUrl('/import_rule/detail/index',array('id'=>$data['import_id'])));
			}else{
				$this->runShowMessge("index",'操作失败');;
			}
		}else{
			$this->render('update',array(
			'data'	=> $model->attributes
			));
		}
	}
	//添加
	public function actionAdd(){
		//$this->__common();
		$this->layout = "//layouts/main_window";
		$date['id'] = Yii::app()->request->getParam('id');
		if(!empty($_POST['submit'])){
			$conn = Yii::app()->db;
			$data = $_POST['info'];
			$data['status'] = (!empty($data['status']))?$data['status']:0;
			$sql  = 'INSERT INTO {{importrule_detail}} ("import_id","rowname","tablerowname","default","status","remark","add_time")  VALUE(:import_id,:rowname,:tablerowname,:default,:status,:remark,:add_time);';
			$result = $conn->createcommand($sql);
			$result->bindParam(':import_id',$data['import_id'],PDO::PARAM_STR);
			$result->bindParam(':rowname',$data['rowname'],PDO::PARAM_STR);
			$result->bindParam(':tablerowname',$data['tablerowname'],PDO::PARAM_STR);
			$result->bindParam(':default',$data['default'],PDO::PARAM_STR);
			$result->bindParam(':status',$data['status'],PDO::PARAM_STR);
			$result->bindParam(':remark',$data['remark'],PDO::PARAM_STR);
			$result->bindParam(':add_time',time());
			$row = $result->execute();
			if(!empty($row)){
				$this->runShowMessge("index");
				$this->redirect($this->createUrl('/import_rule/detail/index',array('id'=>$data['import_id'])));
			}else{
				$this->runShowMessge("index",'数据添加失败');
			}
		}else{
			$this->render('add',array('date'=>$date	));
		}
	}
	//删除
	public function actionDelete(){
		$id = (int)Yii::app()->request->getparam('id');
		$import_id = (int)Yii::app()->request->getparam('import_id');
		if(!empty($id)){
			$model = ImportruleDetail::model()->findByPk($id);
			if(empty($model)){
				$this->runShowMessge("index",'您要删除的数据不存在');
				exit;
			}
			if ($model->model()->deleteAll('"id"=:id',array(':id'=>$id))) {
				//$this->showmessage("删除成功!");
				$this->redirect($this->createUrl('/import_rule/detail/index',array('id'=>$import_id)));
			} else {
				$this->runShowMessge("index",'删除失败');
			}
		}else{
			$this->runShowMessge("index",'删除失败');
		}
	}
}